#!/bin/sh

function gpio_multiplex()
{
	bspmm 0x10260028 0x1200		# GPIO3_5
	bspmm 0x1026002C 0x1100		# GPIO3_6
	bspmm 0x10260030 0x1100		# GPIO3_7
	bspmm 0x10260034 0x1200		# GPIO4_0
	bspmm 0x10260038 0x1100		# GPIO4_1
	bspmm 0x1026003c 0x1100		# GPIO4_2
	bspmm 0x10260040 0x1100		# GPIO4_3
	bspmm 0x10260044 0x1100		# GPIO3_4

	bspmm 0x11130054 0x1105		# GPIO7_4		S3/60
	bspmm 0x11130050 0x1105		# GPIO7_5
	bspmm 0x1113004c 0x1100		# GPIO7_6
	bspmm 0x11130048 0x1100		# GPIO1_1
	bspmm 0x11130044 0x1202		# GPIO1_3
	bspmm 0x11130040 0x1100		# GPIO1_2
	bspmm 0x1113003c 0x1100		# GPIO1_0
	bspmm 0x11130038 0x1100		# GPIO7_7
	bspmm 0x11130004 0x1200		# GPIO1_4
	bspmm 0x11130000 0x1200		# GPIO1_5
}

GPIO_EXPORT_NUM="29 30 31 32 33 34 35 28 61 62 9 11 10 8 63 12 13"

function key_export()
{
	echo 60 > /sys/class/gpio/export
	echo in > /sys/class/gpio/gpio60/direction
}

function gpio_export()
{
	gpio_multiplex
	key_export

	for dir in $GPIO_EXPORT_NUM
	do
		echo $dir > /sys/class/gpio/export
		echo out > /sys/class/gpio/gpio$dir/direction
		echo 1 > /sys/class/gpio/gpio$dir/value
	done
}

function on_function()
{
	for dir in $GPIO_EXPORT_NUM
	do
		echo 0 > /sys/class/gpio/gpio$dir/value
	done
}

function off_function()
{
	for dir in $GPIO_EXPORT_NUM
	do
		echo 1 > /sys/class/gpio/gpio$dir/value
	done
}

function point_function()
{
	for dir in $GPIO_EXPORT_NUM
	do
		echo 0 > /sys/class/gpio/gpio$dir/value
		sleep 0.1
		echo 1 > /sys/class/gpio/gpio$dir/value
	done
}

count=1

if test ! -f /sys/class/gpio/gpio60/value; then
   gpio_export
fi

while [ $count ]; do
	count=$(cat /sys/class/gpio/gpio60/value)
    echo $count
	
	if [ $count -eq 0 ]; then
		on_function
		sleep 0.5;
		off_function
		point_function
    fi
    sleep 1;
done
